package com.itheima.goods.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.goods.mapper.CategoryMapper;
import com.itheima.goods.pojo.Brand;
import com.itheima.goods.pojo.Category;
import com.itheima.goods.service.CategoryService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {


    @Override
    public Result findAll() {
        List<Category> list = this.list();
        if(list.size()>=0){
            return new Result(true,StatusCode.OK,null,list);
        }
        return new Result(false,StatusCode.ERROR,null,null);
    }

    @Override
    public Result add(Category category) {
        boolean b = this.save(category);
        if(b){
            return new Result(b,StatusCode.OK,null,null);
        }
        return new Result(b,StatusCode.ERROR,null,null);
    }

    @Override
    public Result findPage(Integer page, Integer size) {
        IPage<Category> p = new Page<>(page, size);
        this.page(p);
        PageResult<Category> pageResult = new PageResult<>(p.getTotal(), p.getRecords());
        if(p.getRecords().size()>=0){
            return new Result(true,StatusCode.OK,null,pageResult);
        }
        return new Result(false ,StatusCode.ERROR,null,null);
    }

    @Override
    public Result findById(Integer id) {
        Category category = this.getById(id);
        if(category!=null){
            return new Result(true,StatusCode.OK,null,category);
        }
        return new Result(false,StatusCode.ERROR,null,null);
    }

    @Override
    public Result updateId(Category category, Integer id) {
        boolean b = this.updateById(category);
        if(b){
            return new Result(b,StatusCode.OK,null,null);
        }
        return new Result(b,StatusCode.ERROR,null,null);
    }

    @Override
    public Result delete(Integer id) {
        boolean b = this.removeById(id);
        if(b){
            return new Result(b,StatusCode.OK,null,null);
        }
        return new Result(b,StatusCode.ERROR,null,null);
    }
}
